let socket = {};
let socketIo = require('socket.io');
let { getAllAdminInfo, findAdminByUser, findAdminStocket, findUserStocket, findUserByAdmin } = require('./Tool')
/**
 * 所有的用户
 */
let AdminList = [];
let UserList = [];
socket.getSocketio = function (server) {
    var io = socketIo.listen(server);
    io.on('connection', function (socket) {
        /**
         * 管理员的操作方法
         */
        socket.on('newAdmin', function (data) {
            AdminList.push({
                ...data,
                socket,
                state: '等待',
                to: null
            })
            //给用户发送客服的信息
            getAllAdminInfo(UserList, AdminList)
            socket.on('disconnect', function (data) {
                //客服下线
                let AdminSocket = findAdminStocket(AdminList, socket);
                let UserSocket = findUserByAdmin(UserList, AdminSocket);
                if (UserSocket) {
                    UserSocket.to = null;
                    AdminSocket.to = null;
                    AdminSocket.state = "等待";
                    UserSocket.socket.emit('AdminDown')
                    // AdminSocket.socket.emit('ToAdminDown')
                }
                AdminList.splice(AdminList.findIndex(item => item.uid == AdminSocket.uid), 1)
                getAllAdminInfo(UserList, AdminList);
            });
            socket.on('newMessage', function (data) {
                let AdminSocket = findAdminStocket(AdminList, socket);
                let UserSocket = findUserByAdmin(UserList, AdminSocket);
                UserSocket.socket.emit("sendUserMessage", data);
                AdminSocket.socket.emit("newAdminMesssage", data);
            })
        })
        /**
         * 用户的操作方法
         */
        socket.on('newUser', function (data) {
            UserList.push({
                ...data,
                socket,
                to: null
            });
            socket.on('getAdminInfo', function (cb) {
                let list = null;
                list = AdminList.map(item => {
                    return {
                        name: item.name,
                        uid: item.uid,
                        radio: item.radio,
                        state: item.state
                    }
                })
                cb(list)
            });
            socket.on('disconnect', function (data) {
                //用户下线
                let UserSocket = findUserStocket(UserList, socket);
                let AdminSocket = findAdminByUser(AdminList, UserSocket);
                if (AdminSocket) {
                    UserSocket.to = null;
                    AdminSocket.to = null;
                    AdminSocket.state = "等待";
                    AdminSocket.socket.emit('UserDown')
                    // UserSocket.socket.emit('ToUserDown')
                }
                UserList.splice(UserList.findIndex(item => item.uid == UserSocket.uid), 1);
                getAllAdminInfo(UserList, AdminList);
            });
            socket.on('ChoiceAdmin', function (data, cb) {
                let AdminSocket = AdminList.find(item => {
                    if (item.uid == data.uid) {
                        return true;
                    }
                });
                let UserSocket = UserList.find(item => {
                    if (item.socket.id == socket.id) {
                        return true;
                    }
                })
                AdminSocket.to = UserSocket.uid;
                UserSocket.to = AdminSocket.uid;
                AdminSocket.state = "正在和客户交谈";
                getAllAdminInfo(UserList, AdminList)
                cb({
                    msg: `成功和客服${AdminSocket.name}连接成功`,
                    name: AdminSocket.name
                })
                AdminSocket.socket.emit("ChoiceAdminNotice", {
                    msg: `${UserSocket.name}用户请求帮助,已经成功连接`,
                    name: UserSocket.name,
                    messageList: data.messageList
                })
            });
            socket.on('newMessage', function (data) {
                let UserSocket = findUserStocket(UserList, socket);
                let AdminSocket = findAdminByUser(AdminList, UserSocket);
                AdminSocket.socket.emit("sendAdminMesssage", data);
                UserSocket.socket.emit("newUserMessage", data);
            });
            socket.on('UsePersonal', function (data) {

            });
            socket.on('outAdmin', function (cb) {
                //用户下线
                let UserSocket = findUserStocket(UserList, socket);
                let AdminSocket = findAdminByUser(AdminList, UserSocket);
                if (AdminSocket) {
                    UserSocket.to = null;
                    AdminSocket.to = null;
                    AdminSocket.state = "等待";
                    AdminSocket.socket.emit('UserDown')
                    // UserSocket.socket.emit('ToUserDown')
                }
                UserSocket.socket.emit('AdminDown');
                getAllAdminInfo(UserList, AdminList);
                // UserList.splice(UserList.findIndex(item => item.uid == UserSocket.uid), 1);
            })
        })

        /**
         * 公用方法
         */

    })
}

module.exports = socket;
/**
 *
 * 1-获得所有的在线客服（如果没有保存在数据库中），通过得到socket
 * 2-随机选择一名客服，进行一对一会话，如果没有剩余客服，将提醒用户等待
 */